home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / NeuroSim 1.0.2 / .cp / CNeuroSimApp.cp < prev    next >
Encoding:
Text File  |  1997-03-25  |  9.4 KB  |  332 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CNeuroSimApp.cp                ©1996 Timo Eloranta
  3. // ===========================================================================
  4. //    An application class derived from the PowerPlant LApplication class.
  5. //    This class handles the menu commands and owns the neural net and the
  6. //    window inside of which our pane object is.
  7.  
  8. #include "CNeuroSimApp.h"
  9.  
  10. #include <LApplication.h>        // PowerPlant classes
  11. #include <LCaption.h>            //
  12. #include <LGrowZone.h>            //
  13. #include <LPicture.h>            //
  14. #include <LTabGroup.h>            //
  15. #include <LWindow.h>            //
  16.  
  17. #include <LGAPushButton.h>        // PP grayscale classes
  18. #include <LGAPrimaryBox.h>        //
  19. #include <LGASecondaryBox.h>    //
  20. #include <LGAEditField.h>        //
  21. #include <LGADialogBox.h>        //
  22.  
  23. #include <UDesktop.h>            // Deactivate & Activate
  24. #include <UDrawingState.h>        // UQDGlobals
  25. #include <UModalDialogs.h>        // StDialogHandler
  26. #include <UMemoryMgr.h>            // InitializeHeap
  27. #include <URegistrar.h>            // RegisterClass
  28.  
  29. #include <PP_Messages.h>        // cmd_New, msg_OK
  30.  
  31. #include "CStdNeuralNet.h"        // The only net type we have implemented
  32.  
  33. #include "CNeuroSimPane.h"        // Our pane class
  34. #include "CParamsDialog.h"        // Our dialog class
  35.  
  36. #include "CMyCaption.h"            // My own caption class
  37. #include "CAGASlider.h"            // James Jennings' grayscale slider class
  38.  
  39.  
  40. // ===========================================================================
  41. //        • Main Program
  42. // ===========================================================================
  43. //    A typical main function of a PowerPlant based Mac application
  44.  
  45. void main()
  46. {
  47.                                     // Set Debugging options
  48. #ifdef Debug_Throw
  49.     SetDebugThrow_(debugAction_Alert);
  50. #endif
  51. #ifdef Debug_Signal
  52.     SetDebugSignal_(debugAction_Alert);
  53. #endif
  54.  
  55.     InitializeHeap(3);                // Initialize Memory Manager
  56.     
  57.                                     // Initialize standard Toolbox managers
  58.     UQDGlobals::InitializeToolbox( &qd );
  59.     
  60.                                     // Initialize randomizer seed
  61.     ::GetDateTime((unsigned long *)&qd.randSeed);
  62.     
  63.     new LGrowZone(20000);            // Install a GrowZone function to catch
  64.                                     //  low memory situations.
  65.     
  66.     CNeuroSimApp    theApp;            // Create instance of our application
  67.     theApp.Run();                    //  class and run it
  68. }
  69.  
  70.  
  71. // ===========================================================================
  72. //        • CNeuroSimApp Class
  73. // ===========================================================================
  74.  
  75. // ---------------------------------------------------------------------------
  76. //        • CNeuroSimApp
  77. //
  78. //          Called by:    main()
  79. // ---------------------------------------------------------------------------
  80. //    Constructor
  81.  
  82. CNeuroSimApp::CNeuroSimApp()
  83. {
  84.     RegisterClasses();
  85.  
  86.         // Initialize parameters with default values
  87.         // (the defaults are defined in NS_Types.h).
  88.  
  89.     SGenParams theTemp    
  90.         = { DEFAULT_LENGTH_X_AVG,    DEFAULT_LENGTH_Y_AVG,
  91.             DEFAULT_LENGTH_X_DEV,    DEFAULT_LENGTH_Y_DEV,
  92.             DEFAULT_NET_SIZE,
  93.             DEFAULT_QTY_MIN,        DEFAULT_QTY_MAX };
  94.  
  95.     mParams = theTemp;
  96.     
  97.     mNet    = NULL;
  98.     mWindow = NULL;
  99. }
  100.  
  101.  
  102. // ---------------------------------------------------------------------------
  103. //        • ~CNeuroSimApp
  104. // ---------------------------------------------------------------------------
  105. //    Destructor
  106.  
  107. CNeuroSimApp::~CNeuroSimApp()
  108. {
  109.     if ( mWindow )    delete mWindow;
  110.     if ( mNet )        delete mNet;
  111. }
  112.  
  113.  
  114. // ---------------------------------------------------------------------------
  115. //        • ObeyCommand
  116. //
  117. //          Called by:    LCommander::ProcessCommand
  118. // ---------------------------------------------------------------------------
  119. //    Respond to commands
  120.  
  121. Boolean
  122. CNeuroSimApp::ObeyCommand(
  123.     CommandT    inCommand,
  124.     void        *ioParam)
  125. {
  126.     Boolean            cmdHandled = true;
  127.     CParamsDialog * theDialog;
  128.     
  129.     switch (inCommand) {
  130.     
  131.             // cmd_New, cmd_Params and cmd_Demo are menu commands
  132.     
  133.         case cmd_New:
  134.             InitNewNet();
  135.             break;
  136.  
  137.         case cmd_Params:
  138.             theDialog = (CParamsDialog *)
  139.                 LWindow::CreateWindow( WIND_Params, this);
  140.                 
  141.             theDialog -> InitDialog();
  142.             theDialog -> SetValues( mParams );
  143.             theDialog -> Show();
  144.             break;
  145.  
  146.         case cmd_Demo:
  147.             if ( mNet )
  148.                 mNet -> ChangeDemoMode();
  149.             break;
  150.  
  151.             // cmd_SetParams is the command which is sent by 
  152.             // the parameters dialog if the user presses "OK".
  153.             // We respond to it by reading in the new parameter
  154.             // values before throwing the dialog away.
  155.  
  156.         case cmd_SetParams:
  157.             theDialog = (CParamsDialog *) 
  158.                 ((SLGADialogResponse *) ioParam) -> dialogBox;
  159.                 
  160.             theDialog -> GetValues( mParams );
  161.             delete theDialog;
  162.             break;
  163.     
  164.             // By default we pass the command to our base class
  165.             // which handles cmd_About and cmd_Quit
  166.     
  167.         default:
  168.             cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
  169.             break;
  170.     }
  171.     
  172.     return cmdHandled;
  173. }
  174.  
  175. // ---------------------------------------------------------------------------
  176. //        • FindCommandStatus
  177. //
  178. //          Called by:    LCommander::ProcessCommandStatus
  179. // ---------------------------------------------------------------------------
  180. //    Pass back whether a Command is enabled and/or marked (in a Menu)
  181.  
  182. void
  183. CNeuroSimApp::FindCommandStatus(
  184.     CommandT    inCommand,
  185.     Boolean        &outEnabled,
  186.     Boolean        &outUsesMark,
  187.     Char16        &outMark,
  188.     Str255        outName)
  189. {
  190.     outUsesMark = false;
  191.  
  192.     switch (inCommand) {
  193.  
  194.         case cmd_New:
  195.         case cmd_Params:
  196.             outEnabled = true;
  197.             break;
  198.  
  199.             // The text of the "demo mode" menu item is set dynamically
  200.             // to "Start Demo" or "Stop Demo" depending on whether the
  201.             // mode is currently ON or OFF.
  202.  
  203.         case cmd_Demo:
  204.             if (mNet) {
  205.                 outEnabled = true;
  206.                 if (mNet -> DemoModeOn())
  207.                     ::GetIndString( outName, STRx_Menus, str_StopDemo );
  208.                 else
  209.                     ::GetIndString( outName, STRx_Menus, str_StartDemo );
  210.             } else
  211.                 outEnabled = false;
  212.             break;
  213.     
  214.             // We let our base class handle all the other commands
  215.     
  216.         default:
  217.             LApplication::FindCommandStatus(inCommand, outEnabled, 
  218.                                 outUsesMark, outMark, outName);
  219.             break;
  220.     }
  221. }
  222.  
  223. // ---------------------------------------------------------------------------
  224. //        • InitNewNet
  225. //
  226. //          Called by:    CNeuroSimApp::ObeyCommand
  227. // ---------------------------------------------------------------------------
  228. //    Create a new neural net and the window to display it in.
  229.  
  230. void
  231. CNeuroSimApp::InitNewNet()
  232. {
  233.     CNeuroSimPane *    thePane;
  234.  
  235.         // Throw out the old stuff if any
  236.  
  237.     if ( mWindow )    delete mWindow;
  238.     if ( mNet )        delete mNet;
  239.         
  240.         // Create and initialize a new net object.
  241.         
  242.     mNet = new CStdNeuralNet( mParams );
  243.  
  244.         // Create the main window from a resource
  245.         // (our pane is inside the window)
  246.         
  247.     mWindow    = LWindow::CreateWindow( WIND_NeuroSim, this );
  248.     thePane    = (CNeuroSimPane *) mWindow -> FindPaneByID( pane_NeuralNet );
  249.  
  250.         // "Introduce" the net and the pane to each other.
  251.  
  252.     thePane    -> SetNet( mNet );
  253.     mNet    -> SetPane( thePane );
  254.     
  255.     mWindow -> Show();
  256. }
  257.  
  258. // ---------------------------------------------------------------------------
  259. //        • ShowAboutBox
  260. //
  261. //          Called by:    LApplication::ObeyCommand
  262. // ---------------------------------------------------------------------------
  263. //    Display the About Box for NeuroSim
  264.  
  265. void
  266. CNeuroSimApp::ShowAboutBox()
  267. {
  268.     StDialogHandler        theHandler( WIND_NeuroSimAbout, this );
  269.     LWindow                *theDialog = theHandler.GetDialog();
  270.  
  271.     if ( theDialog ) {
  272.         while ( true ) {
  273.             MessageT    hitMessage = theHandler.DoDialog();
  274.             
  275.             if ( hitMessage == msg_OK )
  276.                 break;
  277.         }
  278.     } else {                            // This is only for the case where
  279.         UDesktop::Deactivate();            // we don't have enough memory
  280.         ::Alert( ALRT_About, nil );        // to show the finer About Box
  281.         UDesktop::Activate();            // requested above...
  282.     }
  283. }
  284.  
  285. // ---------------------------------------------------------------------------
  286. //        • RegisterClasses
  287. //
  288. //          Called by:    CNeuroSimApp::CNeuroSimApp
  289. // ---------------------------------------------------------------------------
  290. //    Register classes for objects created from 'PPob' resources
  291.  
  292. void
  293. CNeuroSimApp::RegisterClasses()
  294. {
  295.             // --- Traditional PowerPlant classes --- //
  296.  
  297.     URegistrar::RegisterClass(    LCaption    ::class_ID,    (ClassCreatorFunc)    
  298.                                 LCaption    ::CreateCaptionStream);
  299.     URegistrar::RegisterClass(    LPane        ::class_ID, (ClassCreatorFunc)
  300.                                 LPane        ::CreatePaneStream);
  301.     URegistrar::RegisterClass(    LPicture    ::class_ID, (ClassCreatorFunc)
  302.                                 LPicture    ::CreatePictureStream);
  303.     URegistrar::RegisterClass(    LTabGroup    ::class_ID, (ClassCreatorFunc)
  304.                                 LTabGroup    ::CreateTabGroupStream);
  305.     URegistrar::RegisterClass(    LView        ::class_ID, (ClassCreatorFunc)
  306.                                 LView        ::CreateViewStream);
  307.     URegistrar::RegisterClass(    LWindow        ::class_ID, (ClassCreatorFunc)
  308.                                 LWindow        ::CreateWindowStream);
  309.  
  310.             // --- PowerPlant's grayscale appearance classes --- //
  311.     
  312.     URegistrar::RegisterClass ( LGAPushButton    ::class_ID, (ClassCreatorFunc)
  313.                                 LGAPushButton    ::CreateFromStream );
  314.     URegistrar::RegisterClass ( LGAPrimaryBox    ::class_ID, (ClassCreatorFunc)
  315.                                 LGAPrimaryBox    ::CreateFromStream );
  316.     URegistrar::RegisterClass ( LGASecondaryBox    ::class_ID, (ClassCreatorFunc)
  317.                                 LGASecondaryBox    ::CreateFromStream );
  318.     URegistrar::RegisterClass ( LGAEditField    ::class_ID, (ClassCreatorFunc)
  319.                                 LGAEditField    ::CreateFromStream );
  320.     URegistrar::RegisterClass ( LGADialogBox    ::class_ID, (ClassCreatorFunc)
  321.                                 LGADialogBox    ::CreateFromStream );
  322.  
  323.             // --- Finally the non-PowerPlant classes --- //
  324.  
  325.     URegistrar::RegisterClass(    CNeuroSimPane    ::class_ID, (ClassCreatorFunc)
  326.                                 CNeuroSimPane    ::CreateNeuroSimPaneStream);
  327.     URegistrar::RegisterClass(    CParamsDialog    ::class_ID, (ClassCreatorFunc)
  328.                                 CParamsDialog    ::CreateParamsDialogStream);
  329.     URegistrar::RegisterClass(    CMyCaption        ::class_ID, (ClassCreatorFunc)
  330.                                 CMyCaption        ::CreateMyCaptionStream);
  331.     CAGASlider::Register();
  332. }